package com.ruoyi.fztz.clxhtb.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.fztz.clxhtb.domain.Clxhtb;
import com.ruoyi.fztz.clxhtb.domain.ClxhtbFj;
import com.ruoyi.fztz.clxhtb.domain.ClxhtbSysb;
import com.ruoyi.fztz.clxhtb.domain.ClxhtbXhcl;
import com.ruoyi.fztz.clxhtb.mapper.ClxhtbMapper;
import com.ruoyi.fztz.clxhtb.service.IClxhtbService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 材料消耗填报Service业务层处理
 *
 * @author huacai
 * @date 2024-11-20
 */
@Service
public class ClxhtbServiceImpl implements IClxhtbService {
    @Resource
    private ClxhtbMapper clxhtbMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询材料消耗填报
     *
     * @param clxhtbId 材料消耗填报主键
     * @return 材料消耗填报
     */
    @Override
    public Clxhtb selectClxhtbByClxhtbId(String clxhtbId) {
        return clxhtbMapper.selectClxhtbByClxhtbId(clxhtbId);
    }

    /**
     * 获取材料消耗填报与实(试)验过程消耗的材料信息
     * @param clxhtbId
     * @return
     */
    @Override
    public Clxhtb selectClxhtbXhclByClxhtbId(String clxhtbId) {
        return clxhtbMapper.selectClxhtbXhclByClxhtbId(clxhtbId);
    }

    /**
     * 获取实(试)验过程使用的设备信息
     * @param clxhtbId
     * @return
     */
    @Override
    public Clxhtb selectClxhtbSySbByClxhtbId(String clxhtbId) {
        return clxhtbMapper.selectClxhtbSySbByClxhtbId(clxhtbId);
    }

    /**
     * 查询材料消耗填报列表
     *
     * @param clxhtb 材料消耗填报
     * @return 材料消耗填报
     */
    @Override
    public List<Clxhtb> selectClxhtbList(Clxhtb clxhtb) {
        return clxhtbMapper.selectClxhtbList(clxhtb);
    }

    /**
     * 查询材料消耗填报列表(用于 研发活动证明(村料消耗)选择 组件)
     * @param clxhtb
     * @return
     */
    @Override
    public List<Clxhtb> selectClXhDxList(Clxhtb clxhtb) {
        return clxhtbMapper.selectClXhDxList(clxhtb);
    }

    /**
     * 新增材料消耗填报
     *
     * @param clxhtb 材料消耗填报
     * @return 结果
     */
    @Transactional
    @Override
    public int insertClxhtb(Clxhtb clxhtb) {
        clxhtb.setCreateTime(DateUtils.getNowDate());
        int rows = clxhtbMapper.insertClxhtb(clxhtb);
        insertClxhtbFj(clxhtb);
        insertClxhtbXhcl(clxhtb);
        insertClxhtbSysb(clxhtb);
        return rows;
    }

    /**
     * 批量新增材料消耗填报
     *
     * @param clxhtbs 材料消耗填报List
     * @return 结果
     */
    @Override
    public int batchInsertClxhtb(List<Clxhtb> clxhtbs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(clxhtbs)) {
            try {
                for (int i = 0; i < clxhtbs.size(); i++) {
                    int row = clxhtbMapper.insertClxhtb(clxhtbs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == clxhtbs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改材料消耗填报
     *
     * @param clxhtb 材料消耗填报
     * @return 结果
     */
    @Transactional
    @Override
    public int updateClxhtb(Clxhtb clxhtb) {
        clxhtb.setUpdateTime(DateUtils.getNowDate());
//        clxhtbMapper.deleteClxhtbFjByClxhtbId(clxhtb.getClxhtbId());
        clxhtbMapper.deleteClxhtbXhclByClxhtbId(clxhtb.getClxhtbId());
        clxhtbMapper.deleteClxhtbSysbByClxhtbId(clxhtb.getClxhtbId());
        insertClxhtbFj(clxhtb);
        insertClxhtbXhcl(clxhtb);
        insertClxhtbSysb(clxhtb);
        return clxhtbMapper.updateClxhtb(clxhtb);
    }

    /**
     * 批量删除材料消耗填报
     *
     * @param clxhtbIds 需要删除的材料消耗填报主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteClxhtbByClxhtbIds(String[] clxhtbIds) {
        clxhtbMapper.deleteClxhtbFjByClxhtbIds(clxhtbIds);
        clxhtbMapper.deleteClxhtbXhclByClxhtbIds(clxhtbIds);
        clxhtbMapper.deleteClxhtbSysbByClxhtbIds(clxhtbIds);
        return clxhtbMapper.deleteClxhtbByClxhtbIds(clxhtbIds);
    }

    /**
     * 删除材料消耗填报信息
     *
     * @param clxhtbId 材料消耗填报主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteClxhtbByClxhtbId(String clxhtbId) {
        clxhtbMapper.deleteClxhtbFjByClxhtbId(clxhtbId);
        clxhtbMapper.deleteClxhtbXhclByClxhtbId(clxhtbId);
        clxhtbMapper.deleteClxhtbSysbByClxhtbId(clxhtbId);
        return clxhtbMapper.deleteClxhtbByClxhtbId(clxhtbId);
    }

    /**
     * 查询某个xmsbId在表中出现过几次
     * @param clxhtbId
     * @return
     */
    @Override
    public AjaxResult selectXmsbIdCountByClxhtb(String clxhtbId) {
        return clxhtbMapper.selectXmsbIdCountByClxhtb(clxhtbId);
    }

    /**
     * 新增附件
     * @param clxhtbFj
     * @return
     */
    @Override
    public int insertClxhtbFjByClxhtbId(ClxhtbFj clxhtbFj) {
        return clxhtbMapper.insertClxhtbFjByClxhtbId(clxhtbFj);
    }

    /**
     * 根据ID查询附件列表
     * @param clxhtbFj
     * @return
     */
    @Override
    public List<ClxhtbFj> selectClxhtbFjList(ClxhtbFj clxhtbFj) {
        return clxhtbMapper.selectClxhtbFjList(clxhtbFj);
    }

    /**
     * 删除材料消耗填报附件
     * @param fjIds
     * @return
     */
    @Override
    public int deleteClxhtbFjByFjIds(Integer[] fjIds) {
        return clxhtbMapper.deleteClxhtbFjByFjIds(fjIds);
    }

    /**
     * 新增材料消耗填报附件信息
     *
     * @param clxhtb 材料消耗填报对象
     */
    public void insertClxhtbFj(Clxhtb clxhtb) {
        List<ClxhtbFj> clxhtbFjList = clxhtb.getClxhtbFjList();
        String clxhtbId = clxhtb.getClxhtbId();
        if (StringUtils.isNotNull(clxhtbFjList)) {
            List<ClxhtbFj> list = new ArrayList<ClxhtbFj>();
            for (ClxhtbFj clxhtbFj : clxhtbFjList) {
                clxhtbFj.setClxhtbId(clxhtbId);
                list.add(clxhtbFj);
            }
//            if (list.size() > 0) {
//                clxhtbMapper.batchClxhtbFj(list);
//            }
        }
    }

    /**
     * 新增实(试)验过程消耗的材料信息
     *
     * @param clxhtb 材料消耗填报对象
     */
    public void insertClxhtbXhcl(Clxhtb clxhtb)
    {
        List<ClxhtbXhcl> clxhtbXhclList = clxhtb.getClxhtbXhclList();
        String clxhtbId = clxhtb.getClxhtbId();
        if (StringUtils.isNotNull(clxhtbXhclList))
        {
            List<ClxhtbXhcl> list = new ArrayList<ClxhtbXhcl>();
            for (ClxhtbXhcl clxhtbXhcl : clxhtbXhclList)
            {
                clxhtbXhcl.setClxhtbId(clxhtbId);
                list.add(clxhtbXhcl);
            }
            if (list.size() > 0)
            {
                clxhtbMapper.batchClxhtbXhcl(list);
            }
        }
    }

    /**
     * 新增实(试)验过程使用的设备信息
     *
     * @param clxhtb 材料消耗填报对象
     */
    public void insertClxhtbSysb(Clxhtb clxhtb)
    {
        List<ClxhtbSysb> clxhtbSysbList = clxhtb.getClxhtbSysbList();
        String clxhtbId = clxhtb.getClxhtbId();
        if (StringUtils.isNotNull(clxhtbSysbList))
        {
            List<ClxhtbSysb> list = new ArrayList<ClxhtbSysb>();
            for (ClxhtbSysb clxhtbSysb : clxhtbSysbList)
            {
                clxhtbSysb.setClxhtbId(clxhtbId);
                list.add(clxhtbSysb);
            }
            if (list.size() > 0)
            {
                clxhtbMapper.batchClxhtbSysb(list);
            }
        }
    }
}
